package com.csun.dsms.controller;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.JyUserMapper;
import com.csun.dsms.model.education.*;
import com.csun.dsms.model.huang.HgReminderData;
import com.csun.dsms.service.JyDataService;
import com.csun.dsms.service.JyProblemDepartmentService;
import com.csun.dsms.service.JyProblemDetailService;
import com.csun.dsms.service.JyProblemReportService;
import com.csun.dsms.utils.pt.HuangGangUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/jyReport")
public class JyProblemReportController {

    @Resource
    private JyProblemReportService jyProblemReportService;

    @Resource
    private JyProblemDetailService jyProblemDetailService;

    @Resource
    private JyProblemDepartmentService jyProblemDepartmentService;

    @Resource
    private JyUserMapper jyUserMapper;

    @Authorization
    @RequestMapping(value = "/queryData",method = RequestMethod.POST)
    public Wrapper queryData (@RequestBody JyQueryDto jyQueryDto){
        PageInfo page = jyProblemReportService.queryListWithPage(jyQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,page);
    }


    /**
     */
    @Authorization
    @RequestMapping(value = "/addData",method = RequestMethod.POST)
    public Wrapper addData (@RequestBody JyProblemReport jyProblemReport){
        int result = jyProblemReportService.save(jyProblemReport);
        if (result>0){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
        }else{
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
    }

    /**
     */
    @Authorization
    @RequestMapping(value = "/updateData",method = RequestMethod.POST)
    public Wrapper updateData (@RequestBody JyProblemReport jyProblemReport){
        int result = jyProblemReportService.update(jyProblemReport);
        if (result>0){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
        }else{
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
    }

    /**
     */
    @Authorization
    @RequestMapping(value = "/deleteData",method = RequestMethod.POST)
    public Wrapper deleteData (@RequestBody JyProblemReport jyProblemReport) {
        int result = jyProblemReportService.deleteByKey(jyProblemReport.getId());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }


    @Authorization
    @RequestMapping(value = "/exportData",method = RequestMethod.POST)
    public void exportData (HttpServletResponse response, @RequestBody JyQueryDto jyQueryDto){
       jyProblemReportService.exportData(response,jyQueryDto);
    }


    @Authorization
    @RequestMapping(value = "/queryDetail",method = RequestMethod.POST)
    public Wrapper queryDetail (@RequestBody JyQueryDto jyQueryDto){
        PageInfo page = jyProblemDetailService.queryListWithPage(jyQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,page);
    }


    /**
     */
    @Authorization
    @RequestMapping(value = "/addDetail",method = RequestMethod.POST)
    public Wrapper addDetail (@RequestBody JyProblemDetail jyProblemDetail){
        int result = jyProblemDetailService.save(jyProblemDetail);
        if (result>0){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
        }else{
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
    }

    /**
     */
    @Authorization
    @RequestMapping(value = "/updateDetail",method = RequestMethod.POST)
    public Wrapper updateDetail (@RequestBody JyProblemDetail jyProblemDetail){
        Date date = new Date();
        jyProblemDetail.setUpdateTime(date);
        int result = jyProblemDetailService.update(jyProblemDetail);
        // 合并更新合计的数据
//        jyProblemReportService.dataCombinedCalculations(jyProblemDetail);

        Integer id = jyProblemDetail.getId();
        JyProblemDetail queryJyProblemDetail = new JyProblemDetail();
        queryJyProblemDetail.setId(id);
        JyProblemDetail jyProblemDetail1 = jyProblemDetailService.selectOne(queryJyProblemDetail);
        if (jyProblemDetail1!=null){
            String department = jyProblemDetail1.getDepartment();
            if (StrUtil.isNotBlank(department)){
                JyUser queryJyUser = new JyUser();
                queryJyUser.setDepartment(department);
                List<JyUser> jyUserList = jyUserMapper.select(queryJyUser);
                if (CollectionUtil.isNotEmpty(jyUserList)) {
                    for (JyUser jyUser : jyUserList) {
                        HgReminderData hgReminderData = new HgReminderData();
                        hgReminderData.setIHgUser(jyUser.getHuangGangId());
                        hgReminderData.setReminderPerson(jyUser.getRealName());
                        hgReminderData.setCreatedTime(date);
                        hgReminderData.setReminderDepart(jyUser.getDepartment());
                        hgReminderData.setReminderPhoneNumber(jyUser.getPhone());
                        hgReminderData.setReminderTime(date);
                        hgReminderData.setReminderType(4);
                        hgReminderData.setReminderContent(department+"修改了数据");
                        hgReminderData.setReminderTitle(department+"修改了数据");
                        HuangGangUtil.sendJyMsg(hgReminderData);
                    }
                }
            }
        }

        if (result>0){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
        }else{
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
    }

    /**
     */
    @Authorization
    @RequestMapping(value = "/deleteDetail",method = RequestMethod.POST)
    public Wrapper deleteDetail (@RequestBody JyProblemDetail jyProblemDetail) {
        int result = jyProblemDetailService.deleteByKey(jyProblemDetail.getId());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }



    @Authorization
    @RequestMapping(value = "/queryDepartment",method = RequestMethod.POST)
    public Wrapper queryDepartment (@RequestBody JyQueryDto jyQueryDto){
        PageInfo page = jyProblemDepartmentService.queryListWithPage(jyQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,page);
    }


    /**
     */
    @Authorization
    @RequestMapping(value = "/addDepartment",method = RequestMethod.POST)
    public Wrapper addDepartment (@RequestBody JyProblemDepartment jyProblemDepartment){
        jyProblemDepartment.setUpdateTime(new Date());
        int result = jyProblemDepartmentService.save(jyProblemDepartment);
        if (result>0){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
        }else{
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
    }

    /**
     */
    @Authorization
    @RequestMapping(value = "/updateDepartment",method = RequestMethod.POST)
    public Wrapper updateDetail (@RequestBody JyProblemDepartment jyProblemDepartment){
        jyProblemDepartment.setUpdateTime(new Date());
        int result = jyProblemDepartmentService.update(jyProblemDepartment);
        if (result>0){
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
        }else{
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE,result);
        }
    }

    /**
     */
    @Authorization
    @RequestMapping(value = "/deleteDepartment",method = RequestMethod.POST)
    public Wrapper deleteDepartment (@RequestBody JyProblemDepartment jyProblemDetail) {
        int result = jyProblemDepartmentService.deleteByKey(jyProblemDetail.getId());
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,result);
    }

    @Authorization
    @RequestMapping(value = "/exportBackupData",method = RequestMethod.POST)
    public void exportBackupData (HttpServletResponse response, @RequestBody JSONObject reqJson){
        jyProblemReportService.exportBackupData(response,reqJson);
    }

    @Authorization
    @RequestMapping(value = "/queryBackupData",method = RequestMethod.POST)
    public Wrapper queryBackupData (@RequestBody JSONObject reqJson){
        return jyProblemReportService.queryBackupData(reqJson);
    }

}
